package natsio

import (
	"gitee.com/liukexing/mqbroker/core/driver/config"
	"gitee.com/liukexing/mqbroker/core/driver/logger"
	"gitee.com/liukexing/mqbroker/core/typedef"
)

const (
	ConfigName = "config.core.driver.natsio"
)

var (
	Config     typedef.ConfigInfo
	nodeConfig map[string]typedef.NatsConfig
)

func init() {
	var ok bool
	Config, ok = config.GetConfig(ConfigName)
	if !ok {
		return
	}
	if logger.CheckErr(typedef.ErrInfo{
		Info: Config.Info,
		Err:  typedef.Decode(Config.Extend["nodes"], &nodeConfig),
	}) {
		return
	}
	gcconfig := typedef.GCConfig{
		Enable: false,
	}
	typedef.Decode(Config.Extend["gc"], &gcconfig)
	if gcconfig.Enable {
		go gc(gcconfig.Tick)
	}
}

func CheckModule() bool {
	if !Config.Info.Enable {
		logger.Warn(typedef.CommInfo{Info: Config.Info, Message: "Module is Disable!"})
	}
	return Config.Info.Enable
}

func NewPubSubServer() typedef.PubSubServer {
	return &NatsPubSub{}
}
